package com.yanqu.road.server.cmd.activity.newyear2023.redpacket.cross;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FireWorkProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_FIREWORK_RP_TABLE_OPNE_SYNC, desc = "开桌子同步")
public class CrossFireWorkSyncOpenDataCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        List<Long> userIdList = new ArrayList<>();
        FireWorkProto.FireWorkOpenTableSyncMsg reqMsg = FireWorkProto.FireWorkOpenTableSyncMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        for(FireWorkProto.FireWorkOpenTableTempMsg tableMsg : reqMsg.getTableList()){
            if(!userIdList.contains(tableMsg.getUserId())){
                userIdList.add(tableMsg.getUserId());
            }
        }
        List<GamePlayer> playerList = GamePlayerMgr.getAllOnlinePlayer();
        for(GamePlayer player : playerList){
            if(SystemOpenMgr.systemOpen(player, eSystemId.FireWork2023.getValue())){
                //不同步自己开的
                if(userIdList.contains(player.getUserId())){
                    FireWorkProto.FireWorkOpenTableSyncMsg.Builder syncMsg = FireWorkProto.FireWorkOpenTableSyncMsg.newBuilder();
                    syncMsg.setActivityId(activityId);
                    for(FireWorkProto.FireWorkOpenTableTempMsg tableMsg : reqMsg.getTableList()){
                        if(tableMsg.getUserId() != player.getUserId()){
                            syncMsg.addTable(tableMsg);
                        }
                    }
                    player.sendPacket(Protocol.U_FIREWORK_RP_TABLE_OPNE_SYNC, syncMsg);
                }else{
                    player.sendPacket(Protocol.U_FIREWORK_RP_TABLE_OPNE_SYNC, reqMsg.toBuilder());
                }
            }
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
